package net.stogi.dsper.back.dao.impl;

import java.util.List;

import net.stogi.common.api.dto.OrderBy;
import net.stogi.common.dao.impl.GenericHibernateDaoImpl;
import net.stogi.dsper.api.model.User;
import net.stogi.dsper.api.model.UserProfile;
import net.stogi.dsper.back.dao.UserProfileDao;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class UserProfileHibernateDaoImpl extends GenericHibernateDaoImpl<UserProfile, Long> implements UserProfileDao {

	@Autowired
	public UserProfileHibernateDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<UserProfile> loadByUser(User user, OrderBy... orderBy) {
		assert user != null : "Parameter 'user' can not be null";
		DetachedCriteria criteria = DetachedCriteria.forClass(UserProfile.class);
		criteria.add(Restrictions.eq("user", user));
		addOrder(criteria, orderBy);
		return getHibernateTemplate().findByCriteria(criteria);
	}
}
